package com.zkingsoft.actions.admin;

import com.zkingsoft.services.ChangesService;
import com.zkingsoft.services.ProjUserService;
import com.zkingsoft.services.UsersService;

import java.util.List;

import javax.annotation.Resource;

import com.zkingsoft.actions.common.BaseController;

import org.springframework.stereotype.Controller;

import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.WebUtil;

import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.model.Changes;
import com.zkingsoft.model.ProjUser;
import com.zkingsoft.model.Task;
import com.zkingsoft.model.Users;

import org.springframework.web.bind.annotation.RequestMapping;

import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.constance.Dictionary;

/**
 * @author guchunxia
 * @date 2016-11-15 11:54
 */
@Controller
@RequestMapping(value = "admin/changes")
public class ChangesController extends BaseController {

	@Resource
	private ChangesService currentService;

	@Resource
	private UsersService usersService;
	@Resource
	private ProjUserService projUserService;

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(Changes changes, PaginationVO pageVo) {
		return showList(currentService, changes, pageVo);
	}

	/**
	 * 对客户的问题反馈进行处理
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(Changes changes) {
		int i = 0;
		if (changes.getId() != null) {
			changes.setOptionUserId(getMe().getId());
			i = currentService.modify(changes);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_OK, null, "处理成功");
			}
		}
		return new AjaxResult(AjaxResult.STATUS_ERR, null, "处理失败");
	}

	/**
	 * 进入对客户的问题反馈进行处理的页面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		Changes changes;
		if (id != null) {
			changes = currentService.findById(id);
			WebUtil.getRequest().setAttribute("obj", changes);
		}
		return "admin/changes-form";
	}

	/**
	 * 进入查看界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/showForm")
	public String showForm(Long id) {
		Changes changes;
		if (id != null) {
			changes = currentService.findById(id);
			WebUtil.getRequest().setAttribute("obj", changes);
		}
		return "admin/changes-show";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}

	/**
	 * 创建任务
	 */
	@RequestMapping(value = "/toCreateTask")
	public String saveAndCreateTask(Changes changes) {
		if (changes.getId() != null) {
			changes = currentService.findById(changes.getId());
			Task task = new Task();
			String des = changes.getContent();
			String url = WebUtil.getRequest().getScheme() + "://" + WebUtil.getRequest().getServerName() + ":"
					+ WebUtil.getRequest().getServerPort() + WebUtil.getRequest().getContextPath()
					+ "/admin/changes/showForm?id=" + changes.getId();
			des += "【任务来源，<a target='blank' href='" + url + "'>请点击链接查看</a>】";
			task.setName(changes.getTitle());
			task.setDescribes(des);
			task.setProjId(changes.getProjId());
			task.setCateId(changes.getInterfaceId());
			WebUtil.getRequest().setAttribute("obj", task);

			/*
			 * Users user = new Users();
			 * user.setUserType(Dictionary.USER_TYPE_EMPLOYEE);
			 * user.setIsDeleted("N"); List<Users> userList =
			 * usersService.findByModel(user);
			 * WebUtil.getRequest().setAttribute("userList", userList);
			 */
		}
		return "admin/change-task-form";
	}

	/**
	 * 加载项目开发人员
	 */
	@RequestMapping(value = "/queryUsers")
	public @ResponseBody AjaxResult queryUsers(Long projId) {
		ProjUser projectUser = new ProjUser();
		projectUser.setProjId(projId);
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, projUserService.findByModel(projectUser), 0);
	}
}